package com.yf.dxapp.module.app.controller.admin.appnotice;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import com.yf.dxapp.module.app.controller.admin.appnotice.vo.AppNoticePageReqVO;
import com.yf.dxapp.module.app.controller.admin.appnotice.vo.AppNoticeRespVO;
import com.yf.dxapp.module.app.controller.admin.appnotice.vo.AppNoticeSaveReqVO;
import com.yf.dxapp.module.app.dal.dataobject.appnotice.AppNoticeDO;
import com.yf.dxapp.module.app.service.appnotice.AppNoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 通知公告")
@RestController
@RequestMapping("/dxapp/app-notice")
@Validated
public class AppNoticeController {

    @Resource
    private AppNoticeService appNoticeService;

    @PostMapping("/create")
    @Operation(summary = "创建通知公告")
    @PreAuthorize("@ss.hasPermission('t:app-notice:create')")
    public CommonResult<Long> createAppNotice(@Valid @RequestBody AppNoticeSaveReqVO createReqVO) {
        return success(appNoticeService.createAppNotice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新通知公告")
    @PreAuthorize("@ss.hasPermission('t:app-notice:update')")
    public CommonResult<Boolean> updateAppNotice(@Valid @RequestBody AppNoticeSaveReqVO updateReqVO) {
        appNoticeService.updateAppNotice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除通知公告")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:app-notice:delete')")
    public CommonResult<Boolean> deleteAppNotice(@RequestParam("id") Long id) {
        appNoticeService.deleteAppNotice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得通知公告")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:app-notice:query')")
    public CommonResult<AppNoticeRespVO> getAppNotice(@RequestParam("id") Long id) {
        AppNoticeDO appNotice = appNoticeService.getAppNotice(id);
        return success(BeanUtils.toBean(appNotice, AppNoticeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得通知公告分页")
    @PreAuthorize("@ss.hasPermission('t:app-notice:query')")
    public CommonResult<PageResult<AppNoticeRespVO>> getAppNoticePage(@Valid AppNoticePageReqVO pageReqVO) {
        PageResult<AppNoticeDO> pageResult = appNoticeService.getAppNoticePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppNoticeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出通知公告 Excel")
    @PreAuthorize("@ss.hasPermission('t:app-notice:export')")
    
    public void exportAppNoticeExcel(@Valid AppNoticePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppNoticeDO> list = appNoticeService.getAppNoticePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "通知公告.xls", "数据", AppNoticeRespVO.class,
                        BeanUtils.toBean(list, AppNoticeRespVO.class));
    }

}